package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.poi.StringUtils;
import com.bwie.pojo.TbUser;
import com.bwie.service.TbUserService;
import com.bwie.mapper.TbUserMapper;
import com.bwie.state.CheckEnum;
import com.bwie.utils.R;
import com.bwie.vo.AdminData;
import com.bwie.vo.PageInfoVo;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Enumeration;
import java.util.concurrent.TimeUnit;

/**
* @author 28242
* @description 针对表【tb_user(用户表)】的数据库操作Service实现
* @createDate 2025-04-22 10:15:31
*/
@Service
public class TbUserServiceImpl extends ServiceImpl<TbUserMapper, TbUser>
    implements TbUserService{

    @Autowired
    TbUserMapper tbUserMapper;

    @Autowired
    HttpServletRequest request;

    @Autowired
    StringRedisTemplate redisTemplate;



    @Override
    public R getUserlistByid(Integer id) {

        TbUser tbUser = tbUserMapper.selectById(id);

        if (tbUser==null){
            return R.ERROR(404,"该用户不存在");
        }
        tbUser.setUserPass(null);

        return R.OK(tbUser);
    }

    @Override
    public R AdminLogin(AdminData adminData) {

        TbUser tbUser = tbUserMapper.selectOne(new QueryWrapper<TbUser>()
                .lambda().eq(TbUser::getUserName, adminData.getUserName()));
        if (tbUser==null){
            return R.ERROR(404,"该用户未注册");
        }
        if (!tbUser.getUserPass().equals(adminData.getUserPass())){
            return R.ERROR(500,"密码错误");
        }

        //生产token
        String token = Jwts.builder()
                .signWith(SignatureAlgorithm.HS256, "123456")
                .claim("userId", tbUser.getUserId())
                .claim("userName", tbUser.getUserName())
                .claim("userNick", tbUser.getNickName())
                .compact();

        String rediskey="token-"+tbUser.getUserId();
        redisTemplate.opsForValue().set(rediskey,token,60, TimeUnit.MINUTES);

        tbUser.setToken(token);
        tbUser.setUserPass(null);
        return R.OK(tbUser);
    }

    @Override
    public R userList(PageInfoVo pageInfoVo) {

        //创建分页对象
        Page<TbUser> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());

        QueryWrapper<TbUser> wrapper = new QueryWrapper<>();

        if (StringUtils.isNotEmpty(pageInfoVo.getKeyWord())){
            wrapper.lambda()
                    .like(TbUser::getUserName,pageInfoVo.getKeyWord())
                    .or()
                    .like(TbUser::getNickName,pageInfoVo.getKeyWord())
                    .or().
                    like(TbUser::getUserMobile,pageInfoVo.getKeyWord());

        }

        wrapper.lambda().orderByDesc(TbUser::getUserId);

        Page<TbUser> tbUserPage = tbUserMapper.selectPage(page, wrapper);
        return R.OK(tbUserPage);
    }

    @Override
    public R userReg(TbUser tbUser) {
        //校验用户名是否重复

        TbUser user = tbUserMapper.selectOne(new QueryWrapper<TbUser>().lambda().eq(TbUser::getUserName, tbUser.getUserName()));

        if (user!=null){
            return R.ERROR(404,"用户重复");
        }
        tbUser.setCreateTime(new Date());
        tbUserMapper.insert(tbUser);


        return R.OK();
    }

    @Override
    public R checkUser(TbUser tbUser) {

        //判断用户是否存在
        TbUser user = tbUserMapper.selectById(tbUser.getUserId());

        if (user==null){
            return R.ERROR(404,"用户不存在");
        }

        if (tbUser.getUserStatus().equals(CheckEnum.CHECK_NO.getCode())){
            if (StringUtils.isEmpty(tbUser.getReasion())){
                return R.ERROR(500,"审核驳回必须填写审核原因");
            }
        }

        user.setUserStatus(tbUser.getUserStatus());
        user.setReasion(tbUser.getReasion());
        tbUserMapper.updateById(user);

        return R.OK();
    }

    @Override
    public R getMyInfo() {

        String token = request.getHeader("token");

        if (token==null){
            return R.ERROR(500,"请登录");
        }
        Integer userId = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody().get("userId", Integer.class);
        TbUser tbUser = tbUserMapper.selectById(userId);


        return R.OK(tbUser);
    }

    @Override
    public R updateUser(TbUser tbUser) {

        TbUser user = tbUserMapper.selectById(tbUser.getUserId());
        if (user==null){
            return R.ERROR(404,"用户不存在");
        }

        tbUserMapper.updateById(tbUser);
        return R.OK();
    }


}




